import 'package:flutter/material.dart';
import '../models/todo.dart';
import '../widgets/todo_list.dart';

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final List<Todo> _todos = [
    Todo(title: '示例任务 1'),
    Todo(title: '示例任务 2'),
  ];

  void _addTodo(String title) {
    setState(() {
      _todos.add(Todo(title: title));
    });
  }

  void _toggleTodoStatus(int index) {
    setState(() {
      _todos[index].isDone = !_todos[index].isDone;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('代办事项'),
      ),
      body: TodoList(todos: _todos, toggleStatus: _toggleTodoStatus),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _addTodoDialog();
        },
        child: Icon(Icons.add),
      ),
    );
  }

  void _addTodoDialog() {
    TextEditingController _textController = TextEditingController();
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('添加代办事项'),
          content: TextField(
            controller: _textController,
            decoration: InputDecoration(hintText: '输入任务内容'),
          ),
          actions: <Widget>[
            TextButton(
              child: Text('取消'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: Text('添加'),
              onPressed: () {
                Navigator.of(context).pop();
                _addTodo(_textController.text);
              },
            ),
          ],
        );
      },
    );
  }
}